import { NestFactory } from '@nestjs/core';
import { AppModule } from './app.module';
import { ValidationPipe } from '@nestjs/common';
import { ConfigService } from '@nestjs/config';
import { FormatResponseInterceptor } from './format-response.interceptor';
import { InvokeRecordInterceptor } from './invoke-record.interceptor';
import { UnloginFilter } from './unlogin.filter';
import { CustomExceptionFilter } from './custom-exception.filter';
import { DocumentBuilder, SwaggerModule } from '@nestjs/swagger';
import { NestExpressApplication } from '@nestjs/platform-express';
async function bootstrap() {
  const app = await NestFactory.create<NestExpressApplication>(AppModule);
  // 启用 CORS
  app.enableCors({
    origin: true, // 允许所有域名访问，或者设置为具体的域名，如 ['https://example.com']
    methods: 'GET,HEAD,PUT,PATCH,POST,DELETE', // 允许的 HTTP 方法
    credentials: true, // 允许发送 cookies
  });

  app.useStaticAssets('uploads', {
    prefix: '/uploads',
  });
  app.useGlobalPipes(new ValidationPipe());
  app.useGlobalInterceptors(new FormatResponseInterceptor());
  app.useGlobalInterceptors(new InvokeRecordInterceptor());
  app.useGlobalFilters(new UnloginFilter());
  app.useGlobalFilters(new CustomExceptionFilter());
  const configService = app.get(ConfigService);

  //swagger生成文档
  const config = new DocumentBuilder()
    //这些我是不是也可以抽离成配置,是的没错，方便后续再进行修改
    .setTitle(configService.get('swagger_title'))
    .setDescription(configService.get('swagger_discription'))
    .setVersion(configService.get('swagger_version'))
    .addBearerAuth({
      type: 'http',
      description: '基于jwt的认证',
    })
    .build();
  const document = SwaggerModule.createDocument(app, config);
  SwaggerModule.setup(configService.get('swagger_path'), app, document);
  await app.listen(configService.get('nest_server_port'));
}
bootstrap();
